#!/usr/bin/env python3
"""
调试数据库连接和表结构的脚本
"""

from app.core.database import engine
from sqlalchemy import text

def debug_database():
    print("=== 数据库调试信息 ===")
    
    try:
        with engine.connect() as conn:
            # 检查当前数据库
            result = conn.execute(text('SELECT DATABASE()'))
            db_name = result.scalar()
            print(f"1. 当前数据库: {db_name}")
            
            # 检查所有表
            result = conn.execute(text('SHOW TABLES'))
            tables = [row[0] for row in result.fetchall()]
            print(f"2. 数据库中的表: {tables}")
            
            if not tables:
                print("⚠️  数据库为空，需要初始化数据")
                return False
            
            # 检查核心表
            core_tables = ['questions', 'grades', 'knowledge_points']
            print("\n3. 核心表记录数量:")
            
            for table in core_tables:
                if table in tables:
                    result = conn.execute(text(f'SELECT COUNT(*) FROM {table}'))
                    count = result.scalar()
                    print(f"   {table}: {count} 条记录")
                else:
                    print(f"   {table}: ❌ 表不存在")
            
            return True
            
    except Exception as e:
        print(f"❌ 数据库连接错误: {e}")
        return False

if __name__ == "__main__":
    debug_database()